﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace OTDR_OPT
{
    /// <summary>
    /// CorrectionControl.xaml 的交互逻辑
    /// </summary>
    public partial class CorrectionControl : UserControl
    {
        public CorrectionControl()
        {
            InitializeComponent();
        }

        #region 第一路光功值
        //第一路光功
        static DependencyProperty FirstPowerProperty = DependencyProperty.Register(
            "FirstPower",
            typeof(string),
            typeof(CorrectionControl)
            );

        public string FirstPower
        {
            set { SetValue(FirstPowerProperty,value); }
            get { return (string)GetValue(FirstPowerProperty); }
        }
        #endregion


        #region 第二路光功的值
        //第二路光功
        static DependencyProperty SecondPowerProperty = DependencyProperty.Register(
            "SecondPower",
            typeof(string),
            typeof(CorrectionControl)
            );

        public string SecondPower
        {
            set { SetValue(SecondPowerProperty,value); }
            get { return (string)GetValue(SecondPowerProperty); }
        }
        #endregion

        //波长属性
        static DependencyProperty WavelengthProperty = DependencyProperty.Register(
            "Wavelength",
            typeof(string),
            typeof(CorrectionControl)
            );

        public string Wavelength
        {
            set { SetValue(WavelengthProperty, value); }
            get { return (string)GetValue(WavelengthProperty); }
        }


        //添加按钮被点击
        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            Thickness thisThickness = Margin;
            double thisHeight = 30;
            Grid parentGrid = (Grid)Parent;
            #region 在当前控件下面添加一个控件
            CorrectionControl correctionControl = new CorrectionControl();
            parentGrid.Children.Add(correctionControl);
            correctionControl.HorizontalAlignment = HorizontalAlignment.Left;
            correctionControl.VerticalAlignment = VerticalAlignment.Top;
            double left = thisThickness.Left;
            double top = thisThickness.Top + thisHeight + 5;
            double right = thisThickness.Right;
            double bottom = thisThickness.Bottom;
            //linearControl.Margin = new Thickness(left,top,right,bottom);
            //linearControl.Height = thisHeight;
            #endregion

            #region 移动当前控件下面的控件
            foreach (UIElement element in parentGrid.Children)
            {
                CorrectionControl control = element as CorrectionControl;
                Thickness controlThickness = control.Margin;
                double controlLeft = controlThickness.Left;
                double controlTop = controlThickness.Top;
                double controlRight = controlThickness.Right;
                double controlBottom = controlThickness.Bottom;
                if (controlTop >= top)
                {
                    control.Margin = new Thickness(controlLeft, controlTop + thisHeight + 5, controlRight, controlBottom);
                }
            }
            #endregion
            correctionControl.Margin = new Thickness(left, top, right, bottom);
            correctionControl.Height = thisHeight;
        }


        //添加控件
        public void AddControl()
        {
            AddButton_Click(AddButton, new RoutedEventArgs());
        }


        //删除按钮被点击
        private void RemoveButton_Click(object sender, RoutedEventArgs e)
        {
            Thickness thisThickness = Margin;
            Grid parentGrid = (Grid)Parent;
            double top = thisThickness.Top;
            double thisHeight = Height;
            if (parentGrid.Children.Count < 2)
            {
                return;
            }
            //删除控件
            parentGrid.Children.Remove(this);
            #region 移动当前控件下面的控件
            foreach (UIElement element in parentGrid.Children)
            {
                CorrectionControl control = element as CorrectionControl;
                Thickness controlThickness = control.Margin;
                double controlLeft = controlThickness.Left;
                double controlTop = controlThickness.Top;
                double controlRight = controlThickness.Right;
                double controlBottom = controlThickness.Bottom;
                if (controlTop >= top)
                {
                    control.Margin = new Thickness(controlLeft, controlTop - thisHeight - 5, controlRight, controlBottom);
                }
            }
            #endregion
        }

        //文本框内容发生变化时，如果内容不能转为double，显示红色
        private void TextValueChanged(object sender, TextChangedEventArgs args)
        {
            TextBox senderBox = sender as TextBox;
            string textValue = senderBox.Text.Trim();
            try
            {
                Convert.ToDouble(textValue);
                senderBox.Foreground = Brushes.Black;
            }
            catch
            {
                senderBox.Foreground = Brushes.Red;
            }
        }
    }
}
